package com.sooth.system.log.service;

import com.dream.boot.impl.ServiceImpl;
import com.sooth.common.log.event.LoginLogEvent;
import com.sooth.system.log.view.SysLoginLogBo;
import com.sooth.system.log.view.SysLoginLogVo;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogVo, SysLoginLogBo> implements ISysLoginLogService {

    /**
     * 记录登录信息
     *
     * @param loginLogEvent 登录事件
     */
    @Async
    @EventListener
    public void recordLoginLog(LoginLogEvent loginLogEvent) {
        // 封装对象
        SysLoginLogBo loginLog = new SysLoginLogBo();
        loginLog.setTenantId(loginLogEvent.getTenantId());
        loginLog.setUserName(loginLogEvent.getUsername());
        loginLog.setLoginIp(loginLogEvent.getLoginIp());
        loginLog.setLoginLocation(loginLogEvent.getLoginLocation());
        loginLog.setBrowser(loginLogEvent.getBrowser());
        loginLog.setOs(loginLogEvent.getOs());
        loginLog.setStatus(loginLogEvent.getStatus());
        loginLog.setMsg(loginLogEvent.getMessage());
        loginLog.setLoginTime(new Date());
        super.insert(loginLog);
    }
}
